package com.example.localalbum.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.example.localalbum.common.LocalImageHelper;
import com.example.localalbum.common.LocalImageHelper.LocalFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ImageScrollView extends RecyclerView {

    private Context context;
    private List<LocalFile> pictures = new ArrayList<>();
    private List<String> names = new ArrayList<>();

    public ImageScrollView(Context context) {
        this(context,null);
    }

    public ImageScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public void addFiles(List<LocalFile> files) {
        pictures.addAll(files);
        getAdapter().notifyDataSetChanged();
        LocalImageHelper.getInstance().setCurrentSize(pictures.size());
        files.clear();
        scrollToPosition(pictures.size());
    }

    public List<LocalFile> getFiles(){
        return pictures;
    }

    private void init() {
        setLayoutManager(new GridLayoutManager(context, 1, LinearLayoutManager.HORIZONTAL, false));
        ImageAdapter imageAdapter = new ImageAdapter(context,pictures,names);
        imageAdapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                showViewPager(pos);
            }
        });
        imageAdapter.setOnDeleteListener(new ImageAdapter.OnDeleteListener() {
            @Override
            public void onDelete(int pos) {
                if (onDeleteListener != null) {
                    onDeleteListener.onDelete(pos);
                }
            }
        });
        imageAdapter.setOnAddListener(new ImageAdapter.OnAddListener() {
            @Override
            public void onClick() {
                if (onAddListener != null) {
                    onAddListener.onClick();
                }
            }
        });
        setAdapter(imageAdapter);
        if(!isInEditMode()) {
            LocalImageHelper.getInstance().setCurrentSize(pictures.size());
        }
    }

    private void showViewPager(int index) {
        Intent intent = new Intent(context,PagerView.class);
        intent.putExtra("FILE_LIST",(Serializable) pictures);
        intent.putExtra("INDEX",index);
        context.startActivity(intent);
    }

    public OnDeleteListener onDeleteListener;

    public void setOnDeleteListener(OnDeleteListener onDeleteListener){
        this.onDeleteListener = onDeleteListener;
    }

    public void setNeverShowAddButton(boolean neverShow) {
        ((ImageAdapter)getAdapter()).setNeverShow(neverShow);
    }

    public interface OnDeleteListener{
        void onDelete(int pos);
    }

    public OnAddListener onAddListener;

    public void setOnAddListener(OnAddListener onAddListener){
        this.onAddListener = onAddListener;
    }

    public interface OnAddListener{
        void onClick();
    }
}
